package cn.wit.summit.chwan.adapters;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.facebook.drawee.view.SimpleDraweeView;
import com.flyco.tablayout.SlidingTabLayout;

import java.util.List;

import cn.wit.summit.chwan.R;
import cn.wit.summit.chwan.models.dto.JumpBaseBean;
import cn.wit.summit.chwan.models.response.GamePlayTypeResponse;
import cn.wit.summit.chwan.ui.activity.GameLibraryTwoLevelActivity;
import cn.wit.summit.chwan.utils.IntentUtil;
import cn.wit.summit.chwan.utils.LogUtils;
import cn.wit.summit.chwan.utils.UtilsMy;
import cn.wit.summit.chwan.widgets.MyGridView;

/**
 * Created by Alan on 2018/9/13.
 */

public class GameLibraryTwoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<ViewBean> mDatas;
    private Activity mContext;
    private LayoutInflater inflater;
    private ViewPager viewPager;
    private int p10, p20, p30, p157, p247, p227;
    private GameLibraryTopAdapter mAdapter;

    public GameLibraryTwoAdapter(Activity context, List<ViewBean> datas) {
        this.mContext = context;
        this.mDatas = datas;
        inflater = LayoutInflater.from(mContext);

        p10 = (int) context.getResources().getDimension(R.dimen.wdp10);
        p20 = (int) context.getResources().getDimension(R.dimen.wdp20);
        p30 = (int) context.getResources().getDimension(R.dimen.wdp30);
        p157 = (int) context.getResources().getDimension(R.dimen.wdp157);
        p227 = (int) context.getResources().getDimension(R.dimen.wdp227);
        p247 = (int) context.getResources().getDimension(R.dimen.wdp247);
    }

    public GameLibraryTwoAdapter(Activity context, List<ViewBean> datas, ViewPager viewPager, GameLibraryTopAdapter mAdapter) {
        this.mContext = context;
        this.mDatas = datas;
        this.viewPager = viewPager;
        this.mAdapter = mAdapter;
        inflater = LayoutInflater.from(mContext);

        p10 = (int) context.getResources().getDimension(R.dimen.wdp10);
        p20 = (int) context.getResources().getDimension(R.dimen.wdp20);
        p30 = (int) context.getResources().getDimension(R.dimen.wdp30);
        p157 = (int) context.getResources().getDimension(R.dimen.wdp157);
        p227 = (int) context.getResources().getDimension(R.dimen.wdp227);
        p247 = (int) context.getResources().getDimension(R.dimen.wdp247);
    }

    public enum ViewType {
        TYPE_TOP_HEIGHT1,
        TYPE_TOP_HEIGHT2,
        TYPE_TOP_HEIGHT3,
        TYPE_TOP_HEIGHT4,
        TYPE_BOTTOM,
        TYPE_ITEM,
        TYPE_ITEM_V2,
        TYPE_LINE
    }

    public static class ViewBean {
        ViewType viewType;
        Object obj;
        int po;

        public ViewBean() {
        }

        public ViewBean(ViewType viewType) {
            this.viewType = viewType;
            this.obj = null;
        }

        public ViewBean(ViewType viewType, Object obj) {
            this.viewType = viewType;
            this.obj = obj;
        }

        public ViewBean(ViewType viewType, Object obj, int po) {
            this.viewType = viewType;
            this.obj = obj;
            this.po = po;
        }

        public int getPo() {
            return po;
        }

        public void setPo(int po) {
            this.po = po;
        }

        public ViewType getViewType() {
            return viewType;
        }

        public void setViewType(ViewType viewType) {
            this.viewType = viewType;
        }

        public Object getObj() {
            return obj;
        }

        public void setObj(Object obj) {
            this.obj = obj;
        }
    }

    //设置ITEM类型，可以自由发挥，这里设置item position单数显示item1 偶数显示item2
    @Override
    public int getItemViewType(int position) {
        if (mDatas != null) {
            return mDatas.get(position).getViewType().ordinal();
        }
        return -1;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;

        //加载Item View的时候根据不同TYPE加载不同的布局
        if (viewType == ViewType.TYPE_ITEM.ordinal()) {
            holder = new LibraryTwoHolder(inflater.inflate(R.layout.item_libaray_two_view, parent, false));
        } else if (viewType == ViewType.TYPE_TOP_HEIGHT1.ordinal()) {
            holder = new TopHeightOneHolder(inflater.inflate(R.layout.part_game_lib_top_one_view, parent, false));
        } else if (viewType == ViewType.TYPE_TOP_HEIGHT2.ordinal()) {
            holder = new TopHeightTwoHolder(inflater.inflate(R.layout.part_game_lib_top_two_view, parent, false));
        } else if (viewType == ViewType.TYPE_TOP_HEIGHT3.ordinal()) {
            holder = new TopHeightThreeHolder(inflater.inflate(R.layout.part_game_lib_top_three_view, parent, false));
        } else if (viewType == ViewType.TYPE_TOP_HEIGHT4.ordinal()) {
            holder = new TopHeightFourHolder(inflater.inflate(R.layout.part_game_lib_top_four_view, parent, false));
        } else if (viewType == ViewType.TYPE_ITEM_V2.ordinal()) {
            holder = new LibraryTwoV2Holder(inflater.inflate(R.layout.item_libaray_two_v2_view, parent, false));
        } else if (viewType == ViewType.TYPE_BOTTOM.ordinal()) {
            holder = new BottomHolder(inflater.inflate(R.layout.item_game_library_bottom_view, parent, false));
        } else if (viewType == ViewType.TYPE_LINE.ordinal()) {
            holder = new LineHolder(inflater.inflate(R.layout.item_game_library_line_view, parent, false));
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ViewBean viewBean = mDatas.get(position);
        if (holder instanceof LineHolder) {
        } else if (holder instanceof LibraryTwoHolder) {
            LibraryTwoHolder libraryTwoHolder = (LibraryTwoHolder) holder;
            final GamePlayTypeResponse.ListBean mListBean = (GamePlayTypeResponse.ListBean) viewBean.getObj();

            libraryTwoHolder.itemLibTwoNameTv.setText(mListBean.getTitle());
            libraryTwoHolder.itemLibTwoNumTv.setText(mListBean.getGame_count() + "款");
            libraryTwoHolder.itemLibTwoDicTv.setText(mListBean.getSub_title());

            int po = viewBean.getPo();
            LogUtils.e("tbl>>>po:"+po);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            if (po<3) {
                params.topMargin = p20;
                params.height = p157;
            } else {
                params.height = p157 - p20;
            }
            if (po%3==0) {
                params.weight = p247;
                params.leftMargin = p30;
                params.rightMargin = p10;
            } else if (po%3==1) {
                params.weight = p227;
                params.leftMargin = p10;
                params.rightMargin = p10;
            } else if (po%3==2) {
                params.weight = p247;
                params.leftMargin = p10;
                params.rightMargin = p30;
            }
            libraryTwoHolder.itemLibTwoRootRl.setLayoutParams(params);

            String colorStr = mListBean.getBg_colors();
            if (!colorStr.startsWith("#")) {
                colorStr = "#" + colorStr;
            }
            if (colorStr.length() < 7) {
                colorStr = "#cccccc";
            }
            libraryTwoHolder.itemLibTwoRootRl.setBackgroundColor(Color.parseColor(colorStr));
            libraryTwoHolder.itemLibTwoRootRl.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, GameLibraryTwoLevelActivity.class);
                    intent.putExtra("typeId", mListBean.getId());
                    intent.putExtra("title", mListBean.getTitle());
                    mContext.startActivity(intent);
                }
            });
        } else if (holder instanceof LibraryTwoV2Holder) {
            LibraryTwoV2Holder libraryTwoV2Holder = (LibraryTwoV2Holder) holder;
            List<GamePlayTypeResponse.ListBean> mListBeanList = (List<GamePlayTypeResponse.ListBean>) viewBean.getObj();

            libraryTwoV2Holder.itemLibV2OneRootRl.setVisibility(View.INVISIBLE);
            libraryTwoV2Holder.itemLibV2TwoRootRl.setVisibility(View.INVISIBLE);
            libraryTwoV2Holder.itemLibV2ThreeRootRl.setVisibility(View.INVISIBLE);
            for (int i=0;i<mListBeanList.size();i++) {
                final GamePlayTypeResponse.ListBean mListBean = mListBeanList.get(i);
                String colorStr = mListBean.getBg_colors();
                if (!colorStr.startsWith("#")) {
                    colorStr = "#" + colorStr;
                }
                if (colorStr.length() < 7) {
                    colorStr = "#cccccc";
                }
                switch (i) {
                    case 0:
                        libraryTwoV2Holder.itemLibV2OneRootRl.setVisibility(View.VISIBLE);
                        libraryTwoV2Holder.itemLibV2OneNameTv.setText(mListBean.getTitle());
                        libraryTwoV2Holder.itemLibV2OneNumTv.setText(mListBean.getGame_count() + "款");
                        libraryTwoV2Holder.itemLibV2OneDicTv.setText(mListBean.getSub_title());
                        libraryTwoV2Holder.itemLibV2OneRootRl.setBackgroundColor(Color.parseColor(colorStr));
                        libraryTwoV2Holder.itemLibV2OneRootRl.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent = new Intent(mContext, GameLibraryTwoLevelActivity.class);
                                intent.putExtra("typeId", mListBean.getId());
                                intent.putExtra("title", mListBean.getTitle());
                                mContext.startActivity(intent);
                            }
                        });
                        break;
                    case 1:
                        libraryTwoV2Holder.itemLibV2TwoRootRl.setVisibility(View.VISIBLE);
                        libraryTwoV2Holder.itemLibV2TwoNameTv.setText(mListBean.getTitle());
                        libraryTwoV2Holder.itemLibV2TwoNumTv.setText(mListBean.getGame_count() + "款");
                        libraryTwoV2Holder.itemLibV2TwoDicTv.setText(mListBean.getSub_title());
                        libraryTwoV2Holder.itemLibV2TwoRootRl.setBackgroundColor(Color.parseColor(colorStr));
                        libraryTwoV2Holder.itemLibV2TwoRootRl.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent = new Intent(mContext, GameLibraryTwoLevelActivity.class);
                                intent.putExtra("typeId", mListBean.getId());
                                intent.putExtra("title", mListBean.getTitle());
                                mContext.startActivity(intent);
                            }
                        });
                        break;
                    case 2:
                        libraryTwoV2Holder.itemLibV2ThreeRootRl.setVisibility(View.VISIBLE);
                        libraryTwoV2Holder.itemLibV2ThreeNameTv.setText(mListBean.getTitle());
                        libraryTwoV2Holder.itemLibV2ThreeNumTv.setText(mListBean.getGame_count() + "款");
                        libraryTwoV2Holder.itemLibV2ThreeDicTv.setText(mListBean.getSub_title());
                        libraryTwoV2Holder.itemLibV2ThreeRootRl.setBackgroundColor(Color.parseColor(colorStr));
                        libraryTwoV2Holder.itemLibV2ThreeRootRl.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent = new Intent(mContext, GameLibraryTwoLevelActivity.class);
                                intent.putExtra("typeId", mListBean.getId());
                                intent.putExtra("title", mListBean.getTitle());
                                mContext.startActivity(intent);
                            }
                        });
                        break;
                }
            }
        } else if (holder instanceof TopHeightOneHolder) {
            final List<JumpBaseBean> temp = (List<JumpBaseBean>) viewBean.getObj();
            TopHeightOneHolder topHeightHolder = (TopHeightOneHolder) holder;

//            topHeightHolder.gameLibTopIv.setVisibility(View.VISIBLE);
//            topHeightHolder.gameLibTopIv.setImageURI(temp.getJump_cover_big_pic());
//            topHeightHolder.gameLibTopIv.setOnClickListener(new MyListener(mContext, temp));
            UtilsMy.setRecommendGoods(mContext, topHeightHolder.vp_recommend, temp, true);
        } else if (holder instanceof TopHeightFourHolder) {
            final JumpBaseBean temp = (JumpBaseBean) viewBean.getObj();
            TopHeightFourHolder topHeightHolder = (TopHeightFourHolder) holder;

            topHeightHolder.gameLibTopIv.setVisibility(View.VISIBLE);
            topHeightHolder.gameLibTopIv.setImageURI(temp.getJump_cover_big_pic());
            topHeightHolder.gameLibTopIv.setOnClickListener(new MyListener(mContext, temp));
        } else if (holder instanceof TopHeightTwoHolder) {
            TopHeightTwoHolder topHeightHolder = (TopHeightTwoHolder) holder;
            topHeightHolder.gameLibGv.setAdapter(mAdapter);
        } else if (holder instanceof TopHeightThreeHolder) {
            TopHeightThreeHolder topHeightHolder = (TopHeightThreeHolder) holder;
            if (viewPager!=null) {
                topHeightHolder.activityDetailTabs.setViewPager(viewPager);
                topHeightHolder.activityDetailTabs.setCurrentTab(viewPager.getCurrentItem());
            }
        } else if (holder instanceof BottomHolder) {
            BottomHolder bottomHolder = (BottomHolder) holder;
            JumpBaseBean temp = (JumpBaseBean) viewBean.getObj();

            bottomHolder.gameLibTopIv.setImageURI(temp.getJump_cover_big_pic());
            bottomHolder.gameLibTopIv.setOnClickListener(new MyListener(mContext, temp));
        }
    }

    private class MyListener implements View.OnClickListener {

        private Activity context;
        private JumpBaseBean tempBean;

        public MyListener(Activity context, JumpBaseBean temp) {
            this.context = context;
            this.tempBean = temp;
        }

        @Override
        public void onClick(View v) {
            IntentUtil.getInstance().intentTo(context, tempBean);
        }
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    public class LibraryTwoHolder extends RecyclerView.ViewHolder {
        RelativeLayout itemLibTwoRootRl;
        TextView itemLibTwoNameTv;
        TextView itemLibTwoNumTv;
        TextView itemLibTwoDicTv;

        public LibraryTwoHolder(View view) {
            super(view);
            itemLibTwoRootRl = view.findViewById(R.id.itemLibTwoRootRl);
            itemLibTwoNameTv = view.findViewById(R.id.itemLibTwoNameTv);
            itemLibTwoNumTv = view.findViewById(R.id.itemLibTwoNumTv);
            itemLibTwoDicTv = view.findViewById(R.id.itemLibTwoDicTv);
        }
    }

    public class LineHolder extends RecyclerView.ViewHolder {

        public LineHolder(View view) {
            super(view);
        }
    }

    public class BottomHolder extends RecyclerView.ViewHolder {
        LinearLayout playTypeLl;
        SimpleDraweeView gameLibTopIv;

        public BottomHolder(View view) {
            super(view);
            playTypeLl = view.findViewById(R.id.playTypeLl);
            gameLibTopIv = view.findViewById(R.id.gameLibTopIv);
        }
    }

    public class TopHeightOneHolder extends RecyclerView.ViewHolder {
        SimpleDraweeView gameLibTopIv;
        ConvenientBanner vp_recommend;

        public TopHeightOneHolder(View view) {
            super(view);
            gameLibTopIv = view.findViewById(R.id.gameLibTopIv);
            vp_recommend = view.findViewById(R.id.vp_recommend);
        }
    }

    public class TopHeightFourHolder extends RecyclerView.ViewHolder {
        SimpleDraweeView gameLibTopIv;

        public TopHeightFourHolder(View view) {
            super(view);
            gameLibTopIv = view.findViewById(R.id.gameLibTopIv);
        }
    }

    public class TopHeightTwoHolder extends RecyclerView.ViewHolder {
        MyGridView gameLibGv;

        public TopHeightTwoHolder(View view) {
            super(view);
            gameLibGv = view.findViewById(R.id.gameLibGv);
        }
    }

    public class TopHeightThreeHolder extends RecyclerView.ViewHolder {
        SlidingTabLayout activityDetailTabs;

        public TopHeightThreeHolder(View view) {
            super(view);
            activityDetailTabs = view.findViewById(R.id.activityDetailTabs);
        }
    }

    public class LibraryTwoV2Holder extends RecyclerView.ViewHolder {
        RelativeLayout itemLibV2OneRootRl;
        TextView itemLibV2OneNameTv;
        TextView itemLibV2OneNumTv;
        TextView itemLibV2OneDicTv;
        RelativeLayout itemLibV2TwoRootRl;
        TextView itemLibV2TwoNameTv;
        TextView itemLibV2TwoNumTv;
        TextView itemLibV2TwoDicTv;
        RelativeLayout itemLibV2ThreeRootRl;
        TextView itemLibV2ThreeNameTv;
        TextView itemLibV2ThreeNumTv;
        TextView itemLibV2ThreeDicTv;

        public LibraryTwoV2Holder(View view) {
            super(view);
            itemLibV2OneRootRl = view.findViewById(R.id.itemLibV2OneRootRl);
            itemLibV2OneNameTv = view.findViewById(R.id.itemLibV2OneNameTv);
            itemLibV2OneNumTv = view.findViewById(R.id.itemLibV2OneNumTv);
            itemLibV2OneDicTv = view.findViewById(R.id.itemLibV2OneDicTv);
            itemLibV2TwoRootRl = view.findViewById(R.id.itemLibV2TwoRootRl);
            itemLibV2TwoNameTv = view.findViewById(R.id.itemLibV2TwoNameTv);
            itemLibV2TwoNumTv = view.findViewById(R.id.itemLibV2TwoNumTv);
            itemLibV2TwoDicTv = view.findViewById(R.id.itemLibV2TwoDicTv);
            itemLibV2ThreeRootRl = view.findViewById(R.id.itemLibV2ThreeRootRl);
            itemLibV2ThreeNameTv = view.findViewById(R.id.itemLibV2ThreeNameTv);
            itemLibV2ThreeNumTv = view.findViewById(R.id.itemLibV2ThreeNumTv);
            itemLibV2ThreeDicTv = view.findViewById(R.id.itemLibV2ThreeDicTv);
        }
    }
}
